﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Browsers;
using System.Data;
using ProgNetComponents.Data.SqlCe;

namespace BuildHelper.Setup.ProcedurySql.CustomBrowsers
{
    public static class BrowserTools
    {
        public static void RegisterBrowsers(BrowserDataSet ds)
        {
            DataRow[] rows = ds.STP_CUSTOM_BROWSER.Select();
            string browserId;
            foreach (DataRow r in rows)
            {
                browserId = "Custom" + r["STP_CB_ID"];
                if (!BrowserCache.RegisteredBrowsers.Contains(browserId))
                {
                    Browser b = new Browser();
                    b.BrowserId = browserId;
                    b.Caption = r["STP_CB_NAZWA"].AsString();
                    b.Columns.Add(new BrowserColumn("STP_BRD_KOD", ProgNetComponentsDx.Enums.FilterType.Contains));
                    b.Columns.Add(new BrowserColumn("STP_BRD_NAZWA", ProgNetComponentsDx.Enums.FilterType.Contains));
                    b.DisplayMember = "STP_BRD_NAZWA";
                    b.Filter = "STP_BRD_BROWSER = " + r["STP_CB_ID"].AsString();
                    b.Name = "Custom" + r["STP_CB_ID"].AsString();
                    b.Tables = new string[] { "STP_BROWSER_DATA" };
                    b.ValueMember = "STP_BRD_KOD";
                    BrowserCache.RegisterBrowser(b);
                }
                else
                {
                    var browser = BrowserCache.GetBrowser(browserId, false);
                    if (browser != null)
                        browser.Caption = r["STP_CB_NAZWA"].AsString();
                }
            }
        }
        public static void RegisterCustomBrowsers()
        {
            BrowserDataSet ds = new BrowserDataSet();
            using (SqlCeDataManager m = new SqlCeDataManager())
            {
                m.FillTable(ds.STP_CUSTOM_BROWSER, "SELECT * FROM STP_CUSTOM_BROWSER");
            }
            RegisterBrowsers(ds);
        }
    }
}
